package util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.Key;

/**
 * https://blog.csdn.net/janronehoo/article/details/7590778
 * Created by yzy on 2020-10-23 15:41
 */
public class DESUtil {

    // DES秘钥
    private static final String DES_KEY_51JOB = "lagoukey";
    // DES秘钥向量
    private static final String IV_PARAMETER_51JOB = "lagoukey";
    // 加密算法
    private static final String ALGORITHM = "DES";
    // 编码方式
    private static final Charset CHARSET = Charset.forName("UTF-8");
    // 加密/解密算法-工作模式-填充模式
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";

    private static final String DES_SECRET_51JOB = "bWFnbnVrZHk=";

    public static Key generateKey(String desKey) throws Exception {
        DESKeySpec dks = new DESKeySpec(desKey.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }


    public static String generateKeyStr(String desKey) throws Exception {
        DESKeySpec dks = new DESKeySpec(desKey.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(dks);
        byte[] encoded = secretKey.getEncoded();
        return Base64.encodeBase64String(encoded);  // 将密钥base64编码
    }

    public static String encrypt(String data, String secret, String ivParam){
        if (data == null)
            return null;
        try {
            byte[] desKeyBytes = Base64.decodeBase64(secret);
            SecretKeySpec secretKey = new SecretKeySpec(desKeyBytes, ALGORITHM);

//            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

            byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));
            return Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }

    public static String decrypt(String data, String secret, String ivParam){
        if (data == null)
            return null;
        try {
            byte[] desKeyBytes = Base64.decodeBase64(secret);
            SecretKeySpec secretKey = new SecretKeySpec(desKeyBytes, ALGORITHM);

//            Key secretKey = generateKey();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam.getBytes(CHARSET));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

            byte[] bytes = Base64.decodeBase64(data.getBytes(CHARSET));
            return new String(cipher.doFinal(bytes), CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }
}
